﻿package smooth.core.canvas.gridCanvas {
	import app.core.object.ImageObject;
	import app.core.action.RotatableScalable;
	import app.core.utl.ColorUtil;
	import caurina.transitions.Tweener;
	import flash.display.Stage;
	import flash.display.Shape;		
	import flash.events.Event;
	import flash.geom.Point;	      
	import flash.filters.BitmapFilterQuality;
	import flash.filters.*;
	import flash.events.*;
	import flash.geom.Rectangle;
	import smooth.core.service.gesture.GestureComponent;
	import flash.display.DisplayObjectContainer;
	
	
	public class GridCanvas extends RotatableScalable	{	
		protected var clickgrabber:Shape = new Shape();	
		protected var clickgrabber2:Shape = new Shape();	
		protected var gestureComponent1:GestureComponent;
		private var initialWidth:int;
		private var initialHeight:int;
		private var initialPosX:int;
		private var initialPosY:int;
		private var initialColor:int;
		private var initialAlpha:Number;
			
		public function GridCanvas(posX:int, posY:int, sizeX:int, sizeY:int, color:int, alpha:Number):void {
			// Init des attributs de l'objet
			this.bringToFront = true;			
			this.noScale = false;
			this.noRotate = false;
			this.noMove = false;
			this.initialWidth = sizeX;
			this.initialHeight = sizeY;
			this.initialPosX = posX;
 			this.initialPosY = posY;
			this.initialColor = color;
			this.initialAlpha = alpha;
			// Init des visuels du composant			
			this.initGraphics();
			// Init des events du composant
			this.initEvents();	
			// Init des process du composant
			this.initProcess();
		}	
		
		public function doAnimationFadeIn():void {
			this.scaleX = 0.1;
			this.scaleY = 0.1;
			Tweener.addTween(this, {scaleX:1, scaleY:1, time:0.5, transition:"easeInOutBack" } );	
			this.width = initialWidth;
			this.height = initialHeight;
		}
		
		private function initGraphics():void {
			// Positionnement du canvas
			this.x = initialPosX;
			this.y = initialPosY;
			// Dessin du contour du canvas
			clickgrabber.graphics.lineStyle(2,0xFFFFFF,1);
			clickgrabber.graphics.beginFill(initialColor,this.initialAlpha);
			clickgrabber.graphics.drawRect(0, 0, initialWidth, initialHeight);
			clickgrabber.graphics.endFill();
			this.addChild(clickgrabber);
			// Dessin de la grille
			var spaceX:Number = initialWidth / 10;
			var spaceY:Number = initialHeight / 10;
			
			clickgrabber2.graphics.lineStyle(2,0xFFFFFF,0.5);
			for (var i:int = 1; i < 10; i++) {
				clickgrabber2.graphics.moveTo(i*spaceX, 0);
				clickgrabber2.graphics.lineTo(i*spaceX, initialHeight);
			}
			for (var j:int = 1; j < 10; j++) {
				clickgrabber2.graphics.moveTo(0, j*spaceY);
				clickgrabber2.graphics.lineTo(initialWidth, j*spaceY);
			}
			clickgrabber2.graphics.endFill();
			clickgrabber2.alpha = 0;
			this.addChild(clickgrabber2);
			
			this.gestureComponent1 = new GestureComponent(this, this.width-40, 0, 40, 40, 0xFFFFFF, 1);
			this.addChild(this.gestureComponent1);
		}
		
		private function initProcess():void {
		}
		
		private function initEvents():void {
			this.addEventListener(TouchEvent.MOUSE_DOWN, this.touchDown);						
			this.addEventListener(TouchEvent.MOUSE_UP, this.touchUp);	
		}
		
		
		private function touchDown(e:TouchEvent):void {
			this.clickgrabber2.alpha = 0.5;
			this.gestureComponent1.alpha = 1;
		}
		
		private function touchUp(e:TouchEvent):void {
			this.clickgrabber2.alpha = 0;
			this.gestureComponent1.alpha = 0;
		}
		
		public function removeComponent():void {
			var parentObject1:DisplayObjectContainer = this.parent;
			parentObject1.removeChild(this);
		}
	}
}

